C# 系统托盘图标
WPF NotifyIcon 资料
网址:
http://www.codeproject.com/Articles/36468/WPF-NotifyIcon
https://bitbucket.org/hardcodet/notifyicon-wpf/src
https://www.nuget.org/packages/Hardcodet.NotifyIcon.Wpf/
NuGet:
Install-Package Hardcodet.NotifyIcon.Wpf
NuGet下载的Package:
Hardcodet.NotifyIcon.Wpf.1.0.8.rar
WPF NotifyIcon 使用
一: (不在使用此方法,使用二)
在MainWindow.xaml
xmlns:tb="http://www.hardcodet.net/taskbar"
在MainWindow.xaml的
<tb:TaskbarIcon Name="MyNotifyIcon"
IconSource="Founder.ico"<!--托盘显示图标-->
ToolTipText="Founder 启动本地程序"><!--鼠标移动到托盘图标显示的文字-->
<tb:TaskbarIcon.ContextMenu><!--添加菜单-->
<ContextMenu>
<MenuItem Header="显示" ToolTip="显示主界面" Click="MenuItem_Click_Show" />
<MenuItem Header="退出" ToolTip="退出程序" Click="MenuItem_Click_Close" />
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>
ico文件为16x16 32位,也可以更大
二:
App.xaml
ShutdownMode="OnExplicitShutdown"
的作用是只有在调用Application对象的Shutdown()方法时,应用程序才会关闭。
<Application x:Class="Windowless_Sample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<!--
Note that this application does not have a StartupUri declared, so no Window is automatically loaded.
Also, the ShutdownMode was set to explicit, so we have to close the application programmatically
-->
<!-- merge NotifyIcon and related stuff into the application -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="NotifyIconResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
添加资源NotifyIconResources.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:local="clr-namespace:Windowless_Sample">
<!-- The taskbar context menu - the first row is a dummy to show off simple data binding -->
<!--
The "shared" directive is needed if we reopen the sample window a few times - WPF will otherwise
reuse the same context menu (which is a resource) again (which will have its DataContext set to the old TaskbarIcon)
-->
<ContextMenu x:Shared="false" x:Key="SysTrayMenu">
<MenuItem Header="显示" ToolTip="显示主界面" Command="{Binding ShowWindowCommand}" />
<MenuItem Header="隐藏" ToolTip="隐藏主界面" Command="{Binding HideWindowCommand}" />
<Separator />
<MenuItem Header="退出" ToolTip="退出程序" Command="{Binding ExitApplicationCommand}" />
</ContextMenu>
<!-- the application's NotifyIcon - started from App.xaml.cs. Declares its own view model. -->
<tb:TaskbarIcon x:Key="NotifyIcon"
IconSource="/Red.ico"
ToolTipText="Double-click for window, right-click for menu"
DoubleClickCommand="{Binding ShowWindowCommand}"
ContextMenu="{StaticResource SysTrayMenu}">
<!-- self-assign a data context (could also be done programmatically) -->
<tb:TaskbarIcon.DataContext>
<local:NotifyIconViewModel />
</tb:TaskbarIcon.DataContext>
</tb:TaskbarIcon>
</ResourceDictionary>
NotifyIconViewModel.cs
using System;
using System.Windows;
using System.Windows.Input;
namespace Windowless_Sample
{
/// <summary>
/// Provides bindable properties and commands for the NotifyIcon. In this sample, the
/// view model is assigned to the NotifyIcon in XAML. Alternatively, the startup routing
/// in App.xaml.cs could have created this view model, and assigned it to the NotifyIcon.
/// </summary>
public class NotifyIconViewModel
{
/// <summary>
/// Shows a window, if none is already open.
/// </summary>
public ICommand ShowWindowCommand
{
get
{
return new DelegateCommand
{
CanExecuteFunc = () => Application.Current.MainWindow == null,
CommandAction = () =>
{
Application.Current.MainWindow = new MainWindow();
Application.Current.MainWindow.Show();
}
};
}
}
/// <summary>
/// Hides the main window. This command is only enabled if a window is open.
/// </summary>
public ICommand HideWindowCommand
{
get
{
return new DelegateCommand
{
CommandAction = () => Application.Current.MainWindow.Close(),
CanExecuteFunc = () => Application.Current.MainWindow != null
};
}
}
/// <summary>
/// Shuts down the application.
/// </summary>
public ICommand ExitApplicationCommand
{
get
{
return new DelegateCommand {CommandAction = () => Application.Current.Shutdown()};
}
}
}
/// <summary>
/// Simplistic delegate command for the demo.
/// </summary>
public class DelegateCommand : ICommand
{
public Action CommandAction { get; set; }
public Func<bool> CanExecuteFunc { get; set; }
public void Execute(object parameter)
{
CommandAction();
}
public bool CanExecute(object parameter)
{
return CanExecuteFunc == null || CanExecuteFunc();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}
App.xaml.cs
using System.Windows;
using Hardcodet.Wpf.TaskbarNotification;
namespace Windowless_Sample
{
/// <summary>
/// Simple application. Check the XAML for comments.
/// </summary>
public partial class App : Application
{
private TaskbarIcon notifyIcon;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//create the notifyicon (it's a resource declared in NotifyIconResources.xaml
notifyIcon = (TaskbarIcon) FindResource("NotifyIcon");
}
protected override void OnExit(ExitEventArgs e)
{
notifyIcon.Dispose(); //the icon would clean up automatically, but this is cleaner
base.OnExit(e);
}
}
}